/**
 * https://leetcode.cn/problems/longest-consecutive-sequence/submissions/575069565/
 * 128. 最长连续序列
 * medium,陈贝纯 2024.10.23
 * 并查集
 */

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int n=nums.size();
        int m=0;
        if(n==1)
        return 1;
        for(int i=1;i<n;++i){
            int sum=1;
            while(i<n&&(nums[i-1]==nums[i]-1||nums[i-1]==nums[i])){
                if(nums[i-1]!=nums[i])
                sum++;
                i++;
            }
            m=max(m,sum);
        }
        return m;
    }
};